require(['./config'], () => {
  require(['template', 'jquery', 'header', 'footer'], (template) => {
    class Details {
      constructor () {
        this.Init().then(() => {
          this.scaleImg()
          this.setLocalStorage()
        })
      }

      Init () {
        return new Promise((resolve) => {
          const url = location.search.slice(1).split("&")[0]
          $.get(`https://app2.motie.com/pc/book/${url}/label`, (data) => {
            if (data.error_no == 200) {
              const { name } = data.data.categoryVO
              const { bookName } = data.data
              $('.nav').html(template('nav-template', {
                bookName,
                name
              }))
            }
            $.get(`https://app2.motie.com/pc/book/${url}/intro`, (resp) => {
              console.log(resp)
              if (resp.error_no == 200) {
                const { 
                  authorId,
                  authorName,
                  bookName,
                  content,
                  chapterCount,
                  lastChapterContent,
                  authorSay,
                  lastChapterName,
                  lastChapterTime,
                  bookId
                } = resp.data
                const list = { 
                  authorName,
                  bookName,
                  content,
                  img: location.search.split('&')[1].split('=')[1],
                  words: authorId,
                  words1: authorId.toString().slice(0,5),
                  words2: authorId.toString().slice(0,3),
                  laber: data.data.categoryVO.name
                }
                // const 
                $('.book').html(template('book-template', {
                  ...list
                }))
                $('.mulu').html(template('mulu-template', {
                  chapterCount,
                }))
                $('.summary').html(template('summary-template', {
                  content,
                  authorSay,
                  authorName,
                }))
                $('.newchapter').html(template('newchapter-template', {
                  lastChapterName,
                  lastChapterContent,
                  lastChapterTime,
                }))
                $.get(`https://app2.motie.com/pc/book/${url}/author`, (resp) => {
                  console.log(resp)
                  this.details = {
                    bookName,
                    content: content.slice(0,35) + '...',
                    bookId,
                    price: bookId.toString().slice(0,2),
                    img: location.search.split('&')[1].split('=')[1]
                  }
                  const {
                    authorName,
                    authorPic,
                    bookNum,
                    dayCount,
                    words,
                    books
                  } = resp.data

                  $('.medalbox').html(template('medalbox-template', {
                    authorName,
                    authorPic
                  }))
                  $('.data').html(template('data-template', {
                    bookNum,
                    dayCount,
                    words,
                  }))
                  $('.el-carousel__container').html(template('el-carousel__container-template', {
                    books
                  }))
                  resolve()
              })
              }
            })
          })
        })
      }

      scaleImg () {
        $('.book-middleImg').hover(function () {
          $('.moveBox').css('display','block');
          $('.book-bigImg').css('display','block');
        },function () {
            $('.moveBox').css('display','none');
            $('.book-bigImg').css('display','none');
        });

        $('.book-middleImg').mousemove(function (e) {
          var offset = $(this).offset();
          var X = e.pageX-offset.left-$('.moveBox').width()/2;
          var Y = e.pageY-offset.top-$('.moveBox').height()/2;
          if (X<=0){
              X=0;
          }else if(X>$(this).width()-$('.moveBox').width()){
              X = $(this).width()-$('.moveBox').width();
          }
          if (Y<=0){
              Y=0;
          }else if(Y>$(this).height()-$('.moveBox').height()){
              Y = $(this).height()-$('.moveBox').height();
          }
          $('.moveBox').css('left',X+'px');
          $('.moveBox').css('top',Y+'px');
          $('.book-bigImg img').css('left',-X*2+'px');
          $('.book-bigImg img').css('top',-Y*2+'px');
        });
      }

      setLocalStorage () {
        $('.addshelf').on('click', () => {
          console.log(this.details)
          let books = localStorage.getItem('books')
          if (books) {
            books = JSON.parse(books)
            const exist = books.some((book) => {
              return book.bookId === this.details.bookId
            })
            if (exist) {
              books = books.map((book) => {
                if (book.bookId === this.details.bookId) {
                  book.count++
                }
                return book
              })
            } else {
              books.push({
                ...this.details,
                count: 1,
                check: true
              })
            }
            localStorage.setItem('books', JSON.stringify(books))
          } else {
            localStorage.setItem('books', JSON.stringify([ {
              ...this.details,
              count: 1,
              check: true
            } ]))
          }

          
        })
      }

    }
    new Details()
  })
})